//
//  ZLHomeCell.swift
//  新浪微博
//
//  Created by 朱玉龙 on 9/27/16.
//  Copyright © 2016 朱玉龙. All rights reserved.
//

import UIKit

let commonMargin:CGFloat = 8

let picCellMargin:CGFloat = 3

let maxWidth = screenWidth - 2 * commonMargin

let itemWidth = (maxWidth - 2 * picCellMargin) / 3


class ZLHomeCell: UITableViewCell {

    //头像
    @IBOutlet weak var iv_head: UIImageView!
    //认证
    @IBOutlet weak var iv_avatar: UIImageView!
    //昵称
    @IBOutlet weak var lb_name: UILabel!
    //vip等级
    @IBOutlet weak var iv_menber: UIImageView!
    //发表时间
    @IBOutlet weak var lb_date: UILabel!
    //来源
    @IBOutlet weak var lb_source: UILabel!
    //正文
    @IBOutlet weak var lb_content: UILabel!
    //图片
    @IBOutlet weak var pictureView: ZLPictureView!
    //图片距上约束
    @IBOutlet weak var picLayoutTop: NSLayoutConstraint!
    //图片的宽度约束
    @IBOutlet weak var picLayoutHeight: NSLayoutConstraint!
    //图片的高度约束
    @IBOutlet weak var picLayoutWidth: NSLayoutConstraint!
    //图片的约束
    @IBOutlet weak var picLayout: UICollectionViewFlowLayout!
    //转发的背景
    @IBOutlet weak var repostView: UIView!
    //转发微博的正文
    @IBOutlet weak var lb_repostContent: UILabel!
    //底部工具栏
    @IBOutlet weak var toolbarView: UIView!
    //评论数量
    @IBOutlet weak var btn_comment: UIButton!
    //转发数量
    @IBOutlet weak var btn_repost: UIButton!
    //点赞数量
    @IBOutlet weak var btn_praise: UIButton!
    
//    返回行高的最大值
    func getCellMaxY(model: ZLStatusViewModel) ->CGFloat {
        
          self.statusModel = model
 
        self.contentView.layoutIfNeeded()
        
        let maxY = toolbarView.frame.maxY
        
        return maxY
    }
    
    
    
    var statusModel: ZLStatusViewModel? {
        
        didSet {
            
            iv_head.sd_setImage(with: statusModel?.iconURL)
            iv_avatar.image = statusModel?.avatarImage
            lb_name.text = statusModel?.status?.user?.name
            iv_menber.image = statusModel?.menberImage
            lb_date.text = statusModel?.status?.created_at
            lb_source.text = statusModel?.status?.source
            lb_content.text = statusModel?.status?.text
            lb_content.numberOfLines = 0;
            iv_head.layer.cornerRadius = 20
            iv_head.layer.masksToBounds = true
            
            //转发正文
           
            lb_repostContent?.text = statusModel?.status?.retweeted_status?.text ?? ""
        
            lb_repostContent?.preferredMaxLayoutWidth = maxWidth
            
            setuptoolViewButtonTitlt(button: btn_comment, count: statusModel?.status?.comments_count)
            
            setuptoolViewButtonTitlt(button: btn_repost, count: statusModel?.status?.reposts_count)
            
            setuptoolViewButtonTitlt(button: btn_praise, count: statusModel?.status?.attitudes_count)
            
            //有几张图
            let pictureInfo = statusModel?.pictureInfo
            
            let count = pictureInfo?.count ?? 0
            
            pictureView.pictureInfo = pictureInfo
        
            picLayoutTop.constant = count == 0 ? 0 : commonMargin
            
            let itemSize = changeItemSize(count: count)
            
            picLayoutWidth.constant = itemSize.width
            
            picLayoutHeight.constant = itemSize.height
            
        }
        
        
    }
    
    //设置显示转发的数量
    private func  setuptoolViewButtonTitlt(button: UIButton, count: Int? = 0 ) {
        
        if count == nil && count == 0 {
            
            return
        } else if count! >= 10000 {
            
            let num = CGFloat(count! / 1000) / 10
            
            button.setTitle("\(num)万", for: .normal)
            
            return
        }
        
        button .setTitle("\(count!)", for: .normal)
        
        
    }
    
    //设置flowLayout
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.selectionStyle = .none
        
        lb_content.preferredMaxLayoutWidth = maxWidth
    
        picLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
    
        picLayout.minimumLineSpacing = picCellMargin
        
        picLayout.minimumInteritemSpacing = picCellMargin
    
    }

   
    //定义图片的尺寸
    private func changeItemSize(count:Int) -> CGSize {
        
        if count == 0 {
            
            return CGSize.zero
        }
        
        if count < 4 {
            
            self.picLayout.itemSize = CGSize(width: itemWidth, height: itemWidth * 1.3)
            
            return CGSize(width: maxWidth, height: 1.3 * itemWidth)
        }
        
        if count == 4 {
            
            let width = 2 * itemWidth + picCellMargin
            
            return CGSize(width: width, height: width)
        }
        
        
        let row = CGFloat((count - 1) / 3 + 1)
        
        let height:CGFloat = row * itemWidth + (row - 1) * picCellMargin
        
        return CGSize(width: maxWidth, height: height)
     
    }
    
    
}
